#!/bin/sh

cleanup()
{
	if [ $HASCLEANED -eq 0 ]; then
		
		echo "Clean up chroot jail"
		$SUDO chroot $ROOT /bin/chrootclean

		if [ "$OUTPUT" -a -d "$OUTPUT" ]; then
		    $SUDO umount $ROOT/drv_repo/out
			#sub=`$SUDO ls $ROOT/drv_repo/out`
			#if [ -z "$sub" ]; then
			#    $SUDO rm -r $ROOT/drv_repo/out
			#fi
		fi
		
		if [ "$PATH_DRIVERS" -a -d "$PATH_DRIVERS" ]; then
			$SUDO umount $ROOT/drv_repo/drivers
			#sub=`$SUDO ls $ROOT/drv_repo/drivers`
			#if [ -z "$sub" ]; then
			#    $SUDO rm -r $ROOT/drv_repo/drivers
			#fi
		fi
		
		if [ "$DRV_REPO" -a -d "$DRV_REPO" ]; then
			$SUDO umount $ROOT/drv_repo
		fi
		
		$SUDO umount $CHROOT_ETC
		$SUDO umount $CHROOT_TMP
		$SUDO umount $ROOT/proc
		$SUDO umount $CHROOT_LOCK

		$SUDO rm -f $CHROOT_LOCK
		$SUDO rm -rf $CHROOT_TMP
		$SUDO rm -rf $CHROOT_ETC 
		
		HASCLEANED=1
	fi
}

usage()
{
    echo "Usage: chrootsh [-b <build_root> [-d <drivers>] [-o <output>]] -r <root> [program] [arguments]"
	echo "       root, the root directory of the chroot jail"
	echo "       build_root, the directory where the top level makefile stay"
	echo "       drivers, the directory hold drivers, if drivers is under build_root, it can be omitted"
	echo "       output, the output directory to hold the build result, need work with -d"
    exit 1
}

#set -x

if [ $# -lt 2 ]; then
    usage
fi

while [ "$1" ]
do
    case $1 in
	"-b")
	    DRV_REPO=$2
		shift 2;;
	"-d")
		PATH_DRIVERS=$2
		shift 2;;
	"-o")
		OUTPUT=$2
		shift 2;;
	"-r")
		ROOT=$2
		shift 2
		ARGUS=$*
		shift $#;;
	*)
		usage;;
	esac
done

if [ "$DRV_REPO" -a ! -d "$DRV_REPO" ]; then
	echo "Error: could not find path \"$DRV_REPO\" to set up drv_repo."
	exit 1
fi

if [ "$OUTPUT" ]; then
    if [ -z "$DRV_REPO" ]; then
	    echo "Error: -o must use with -b"
		exit 1
	fi
	if [ ! -d "$OUTPUT" ]; then
		echo "Error: could not find path \"$OUTPUT\""
		exit 1
	fi
fi

if [ "$PATH_DRIVERS" ]; then
	if [ -z "$DRV_REPO" ]; then
		echo "Error: -d must use with -b"
		exit 1
	fi
	if [ ! -d "$PATH_DRIVERS" ]; then
		echo "Error: could not find path \"$PATH_DRIVERS\""
		exit 1
	fi
fi

if [ ! -d "$ROOT" ]; then
    echo "Error: could not change root to \"$ROOT\"."
    exit 1 
fi

if [ $UID -ne 0 ]; then
    SUDO=sudo
fi

HASCLEANED=0

CHROOT_LOCK=`$SUDO mktemp -p /tmp pskmb.chroot.lock.XXXXXX`

$SUDO sh -c "echo 1 > $CHROOT_LOCK"

$SUDO grep -q "1" $ROOT/.chroot.lock
if [ $? -eq 0 ]; then
    echo "ERROR: $ROOT is busy."
    $SUDO rm -f $CHROOT_LOCK
    exit 1    
fi

trap cleanup 0
trap "exit 2" SIGHUP SIGINT SIGQUIT SIGPIPE SIGTERM

$SUDO mount -o bind $CHROOT_LOCK $ROOT/.chroot.lock

$SUDO mount -o bind /proc $ROOT/proc

# we need a writable /tmp
CHROOT_TMP=`$SUDO mktemp -d -p /tmp pskmb.tmp.XXXXXXX`
$SUDO mount -o bind $CHROOT_TMP $ROOT/tmp

# we need a writable /etc
CHROOT_ETC=`$SUDO mktemp -d -p /tmp pskmb.etc.XXXXXXX`
$SUDO chmod 0777 $CHROOT_ETC
#  we do not need all etc
#$SUDO cp -a $ROOT/etc/* $CHROOT_ETC
$SUDO echo > $CHROOT_ETC/mtab
$SUCO cp -a $ROOT/etc/uname.info $CHROOT_ETC
$SUDO mount -o bind $CHROOT_ETC $ROOT/etc

if [ "$DRV_REPO" -a -d "$DRV_REPO" ]; then
    $SUDO mount -o bind $DRV_REPO $ROOT/drv_repo
fi

if [ "$PATH_DRIVERS" -a -d "$PATH_DRIVERS" ]; then
	$SUDO mkdir $ROOT/drv_repo/drivers > /dev/null 2>&1
	$SUDO mount -o bind $PATH_DRIVERS $ROOT/drv_repo/drivers
fi

if [ "$OUTPUT" -a -d "$OUTPUT" ]; then
    $SUDO mkdir $ROOT/drv_repo/out >/dev/null 2>&1 
	$SUDO mount -o bind $OUTPUT $ROOT/drv_repo/out
fi

$SUDO chroot $ROOT $ARGUS

# cleanup will be called when exit (signal handle)
#cleanup
